package create

import (
	"context"
	"gitee.com/kmyss/gf-ex/yscmd"
	"gitee.com/kmyss/gf-ex/yslog"
	"gitee.com/kmyss/ys-cli/internal/cmd/git/bundle"
	"gitee.com/kmyss/ys-cli/internal/model"
	"gitee.com/kmyss/ys-cli/internal/service"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/os/gtime"
)

type Builder struct {
	yscmd.CommandBuilderCore
}

func (b *Builder) Init(ctx context.Context) {
	b.CommandBuilderCore.Init(ctx)
	b.C.Use = "bundle-create"
	b.C.Short = "生成项目的二进制文件"

	b.C.Run = run
	b.addFlag()
}

func (b *Builder) addFlag() {
	b.SetFlags(
		&bundle.FlagBranchBuilder{},
	)
	yscmd.SetDefaultFlag(b)
}

func run(c *yscmd.Command) {
	err := service.Git().BundleCreate(c.Context(), &model.BundleConfigInput{
		Project: gfile.Basename(gfile.Pwd()),
		Branch:  bundle.FlagBranch(c.Context()).Get(),
		Date:    gtime.Now().Format("Ymd_H_i_s"),
	})
	if err != nil {
		yslog.Error(err)
		yscmd.CmdWait(c.Context())
		return
	}

	yscmd.TimeToCloseShell(c.Context(), 2)

}
